package com.shujia.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Code07FileStatus {
    public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
        // 可以在resource目录中添加log4j.properties来控制打印台信息
        Configuration conf = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
        FileStatus fileStatus = fileSystem.getFileStatus(new Path("/data/aaa.txt"));
        Path path = fileStatus.getPath();
        String owner = fileStatus.getOwner();
        // 目录中长度都为0
        long len = fileStatus.getLen();

        BlockLocation[] fileBlockLocations = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
        for (BlockLocation fileBlockLocation : fileBlockLocations) {
            String[] hosts = fileBlockLocation.getHosts();
            for (String host : hosts) {
                System.out.println("host:"+host);
            }
            // 获取当前Block块的偏移量(当前BLock块的在整个文件中的开始长度)
            long offset = fileBlockLocation.getOffset();
            System.out.println(offset);
        }

        System.out.println("path:"+path+" owner:"+owner+" len:"+len);
        fileSystem.close();


    }
    // 作业：1.判断是否为文件或目录
    //      2.迭代删除目录中的多个文件或目录
}
